﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
//using SLIC.Models.Entity;
using System.Web.Security;
using System.Web.SessionState;
using SLIC.Models.EntityModel;

namespace SLIC.Models.Auth
{

    public class RoleAuthorizationService
    {
        // TODO : Change this when new role is added
        static aspnet_Roles[] rolesinDB;
        
        static RoleAuthorizationService()
        {
            try
            {
                using (SLICEntities context = new SLICEntities())
                {
                    rolesinDB = (from p in context.aspnet_Roles select p).ToArray();
                }
            }
            catch (Exception)
            {                
                throw;  
            }
        }

        public static Boolean IsAuthorized(string action_fmt)
        {
            bool status = false;
            try
            {
                string[] userroles = Roles.GetRolesForUser(HttpContext.Current.User.Identity.Name);

                foreach (var userrole in userroles)
                {
                    status= rolesinDB.Any(x => ((x.RoleName == userrole) && (x.Description.Contains(action_fmt))));

                    //for multiple roles
                    if (status)
                    {
                        break;
                    }
                }              
            }
            catch (Exception)
            {
                throw;
            }
            return status;
            //return true;
        }

    }
}